package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.ProFeedback;
import org.example.erp.service.ProFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ProFeedbackController {
    @Autowired
    private ProFeedbackService proFeedbackService;

    @GetMapping("/feedback/list")
    public DataResult list(
            @RequestParam(value = "feedbackType", required = false) String feedbackType,
            @RequestParam(value = "workstationName", required = false) String workstationName,
            @RequestParam(value = "workorderCode", required = false) String workorderCode,
            @RequestParam(value = "itemCode", required = false) String itemCode,
            @RequestParam(value = "itemName", required = false) String itemName,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "currentPage", required = false) Integer currentPage
    ) {
        Page<ProFeedback> page = new Page<>(currentPage, pageSize);
        QueryWrapper<ProFeedback> queryWrapper = new QueryWrapper<>();
        if (feedbackType != null && !"".equals(feedbackType)) {
            queryWrapper.lambda().eq(ProFeedback::getFeedbackType, feedbackType);
        }
        if (workstationName != null && !"".equals(workstationName)) {
            queryWrapper.lambda().like(ProFeedback::getWorkstationName, workstationName);
        }
        if (workorderCode != null && !"".equals(workorderCode)) {
            queryWrapper.lambda().eq(ProFeedback::getWorkorderCode, workorderCode);
        }
        if (itemCode != null && !"".equals(itemCode)) {
            queryWrapper.lambda().eq(ProFeedback::getItemCode, itemCode);
        }
        if (itemName != null && !"".equals(itemName)) {
            queryWrapper.lambda().like(ProFeedback::getItemName, itemName);
        }
        if (status != null && !"".equals(status)) {
            queryWrapper.lambda().eq(ProFeedback::getStatus, status);
        }
        queryWrapper.orderByDesc("record_id");
        page = proFeedbackService.page(page, queryWrapper);
        return DataResult.success(page);
    }

    @PostMapping("/feedback/add")
    public DataResult add(@RequestBody ProFeedback proFeedback) {
        boolean save = proFeedbackService.save(proFeedback);
        return DataResult.success(save ? "成功" : "失败");
    }

    @DeleteMapping("/feedback/delete")
    public DataResult delete(@RequestBody List<Integer> id) {
        boolean b = proFeedbackService.removeByIds(id);
        return DataResult.success(b ? "成功" : "失败");
    }

    @PutMapping("/feedback/update")
    public DataResult update(@RequestBody ProFeedback proFeedback) {
        boolean b;
        if ("FINISHED".equals(proFeedback.getStatus())) {
            b = proFeedbackService.updateDetail(proFeedback);
        } else {
            b = proFeedbackService.updateById(proFeedback);
        }
        return DataResult.success(b ? "成功" : "失败");
    }
}
